package com.liao.healthydiet.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.liao.healthydiet.R;
import com.liao.healthydiet.domain.Food;
import com.liao.healthydiet.ui.adapter.ListViewFoodLayoutAdapter;
import com.liao.healthydiet.utils.GetData;

import java.util.ArrayList;
import java.util.Collections;

public class FoodListActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemClickListener {

    private EditText et_query;
    private Button btn_search;
    private Button btn_clear;
    private ListView listView;

    private ListViewFoodLayoutAdapter adapter;
    private ArrayList<Food> foods;
    private ArrayList<Food> beforeFoods;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_foot_list);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        initView();
        initData();
    }

    private void initView() {
        et_query = (EditText) findViewById(R.id.et_query);
        btn_search = (Button) findViewById(R.id.btn_search);
        btn_clear = (Button) findViewById(R.id.btn_clear);
        listView = (ListView) findViewById(R.id.listView);

        foods = new ArrayList<>();
        beforeFoods = new ArrayList<>();
        adapter = new ListViewFoodLayoutAdapter(this, foods);
        listView.setAdapter(adapter);

        btn_search.setOnClickListener(this);
        btn_clear.setOnClickListener(this);
        listView.setOnItemClickListener(this);
    }

    private void initData() {
        String[] names = GetData.food;
        String[] collocations = GetData.food1;
        String[] details = GetData.foodjianjie;
        int[] images = GetData.resId;
        for (int i = 0; i < names.length; i++) {
            Food food = new Food();
            food.setName(names[i]);
            food.setCollocation(collocations[i]);
            food.setDetail(details[i]);
            food.setImage(images[i]);
            foods.add(food);
            beforeFoods.add(food);
        }
        adapter.notifyDataSetChanged();
    }

    private void queryData() {
        String s = et_query.getText().toString().trim();
        if (TextUtils.isEmpty(s)) {
            return;
        }

        ArrayList<Food>list = new ArrayList<>();
        for (int i = 0; i < beforeFoods.size(); i++) {
            Food food = foods.get(i);
            String name = food.getName();
            if (!name.contains(s)) {
                list.add(food);
            }
        }
        for (Food food : list) {
            foods.remove(food);
        }
        adapter.notifyDataSetChanged();
    }

    private void clearData() {
        et_query.setText("");
        foods.clear();
        foods.addAll(beforeFoods);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_search:
                queryData();
                break;
            case R.id.btn_clear:
                clearData();
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        startActivity(new Intent(this, FoodDetailActivity.class).putExtra("food", foods.get(position)));
    }
}
